package lh.toolclass;

public class HttpTurnClass {
    public HttpTurnClass() {
        turnArray = new TurnClass[]{
                new TurnClass(' ', (char) 0x20)
                , new TurnClass('\'', (char) 0x22)
                , new TurnClass('#', (char) 0x23)
                , new TurnClass('%', (char) 0x25)
                , new TurnClass('&', (char) 0x26)
                , new TurnClass('(', (char) 0x28)
                , new TurnClass(')', (char) 0x29)
                , new TurnClass('+', (char) 0x2B)
                , new TurnClass(',', (char) 0x2C)
                , new TurnClass('/', (char) 0x2F)
                , new TurnClass(':', (char) 0x3A)
                , new TurnClass(';', (char) 0x3B)
                , new TurnClass('<', (char) 0x3C)
                , new TurnClass('=', (char) 0x3D)
                , new TurnClass('>', (char) 0x3E)
                , new TurnClass('?', (char) 0x3F)
                , new TurnClass('@', (char) 0x40)
                , new TurnClass('\\', (char) 0x5C)
                , new TurnClass('|', (char) 0x7C)
        };
    }

    private final TurnClass[] turnArray;

    /**
     * 将常规字符串，转成带转义符的字符串
     *
     * @param content
     * @return
     */
    public String toNewChar(String content) {
        for (TurnClass row :
                turnArray) {
            content = content.replace(row.getOldChar(), row.getNewChar());
        }
        return content;
    }

    /**
     * 将带转义符的字符串，转成常规字符串
     *
     * @param content
     * @return
     */
    public String toOldChar(String content) {
        for (TurnClass row :
                turnArray) {
            content = content.replace(row.getNewChar(), row.getOldChar());
        }
        return content;
    }


    private class TurnClass {
        private char oldChar;
        private char newChar;

        public TurnClass(char oldChar, char newChar) {
            this.oldChar = oldChar;
            this.newChar = newChar;
        }

        public char getOldChar() {
            return oldChar;
        }

        public char getNewChar() {
            return newChar;
        }
    }
}
